﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LibrarySystem.Models;
using LibrarySystem.LibraryUtils.Entities;
using System.Configuration;
using System.Web.Security;

namespace LibrarySystem.LibraryUtils.DAL.DAOs
{
    public class Library_Students : Common
    {

        public List<StudentModel> getStuToComboBox(string stuCode = null)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();

            IEnumerable<Library_Student> queryStu = _context.Library_Students;
            //Filter
            if (!string.IsNullOrEmpty(stuCode))
            {
                queryStu = queryStu.Where(e => e.Code.Contains(stuCode));
            }
            List<Library_Student> students = queryStu.ToList();

            List<StudentModel> stuList = new List<StudentModel>();

            foreach (Library_Student stu in students)
            {
                StudentModel data = new StudentModel();
                data.ID = stu.Id;
                data.StudentCode = stu.Code;
                stuList.Add(data);
            }
            return stuList;
        }

        public int GetDataCount(string studentCode = null, string name = null, string classID = null)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            //Filtering
            IEnumerable<Library_Student> queryStu = _context.Library_Students;
            if (!string.IsNullOrEmpty(studentCode))
            {
                queryStu = queryStu.Where(s => s.Code.Contains(studentCode));
            }
            if (!string.IsNullOrEmpty(name))
            {
                queryStu = queryStu.Where(s => s.FirstName.Contains(name));
            }
            if (!string.IsNullOrEmpty(classID))
            {
                queryStu = queryStu.Where(s => s.ClassID.Contains(classID));
            }
            return queryStu.Count();
        }

        public List<StudentModel> GetDataList(int startIndex = 0, int pageSize = 0, string sorting = null,
            string studentCode = null, string name = null, string classID = null)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            //Filtering
            IEnumerable<Library_Student> queryStu = _context.Library_Students;
            if (!string.IsNullOrEmpty(studentCode))
            {
                queryStu = queryStu.Where(s => s.Code.Contains(studentCode));
            }
            if (!string.IsNullOrEmpty(name))
            {
                queryStu = queryStu.Where(s => s.FirstName.Contains(name));
            }
            if (!string.IsNullOrEmpty(classID))
            {
                queryStu = queryStu.Where(s => s.ClassID.Contains(classID));
            }
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("Code ASC"))
            {
                queryStu = queryStu.OrderBy(s => s.Code);
            }
            else if (sorting.Equals("Code DESC"))
            {
                queryStu = queryStu.OrderByDescending(s => s.Code);
            }
            List<Library_Student> students = pageSize > 0 ?
                                             queryStu.Skip(startIndex).Take(pageSize).ToList()
                                             : queryStu.ToList();
            List<StudentModel> stuList = new List<StudentModel>();
            foreach (Library_Student stu in students)
            {
                StudentModel data = new StudentModel();
                data.ID = stu.Id;
                data.UserName = stu.aspnet_User.UserName;
                data.StudentCode = stu.Code;
                data.FirstName = stu.FirstName;
                data.LastName = stu.LastName;
                data.Birthday = (DateTime)stu.BirthDay;
                data.Email = stu.Email;
                data.ClassID = stu.ClassID;
                stuList.Add(data);
            }
            _context.Dispose();
            return stuList;
        }

        public bool CheckStudentRoll(string studentRoll)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            int count = _context.Library_Students.Where(s => s.Code == studentRoll).Count();
            if (count == 0)
                return false;

            return true;
        }

        public StudentModel AddNewData(StudentModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Student stu = new Library_Student();
            stu.UserId = base.CreateUserAccount(data.UserName, ConfigurationManager.AppSettings["DefaultPassword"],
                ConfigurationManager.AppSettings["StudentRole"]);
            stu.Code = data.StudentCode;
            stu.FirstName = data.FirstName;
            stu.LastName = data.LastName;
            stu.BirthDay = (DateTime)data.Birthday;
            stu.Email = data.Email;
            stu.ClassID = data.ClassID;
            //Them moi vao database
            _context.Library_Students.InsertOnSubmit(stu);
            _context.SubmitChanges();
            //Gan gia tri ID vua moi tao ra cho Model
            data.ID = stu.Id;
            _context.Dispose();
            return data;
        }

        public bool UpdateData(StudentModel data)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Student stu = _context.Library_Students.Where(s => s.Id == data.ID).FirstOrDefault();
            //Assign parameter
            stu.Code = data.StudentCode;
            stu.FirstName = data.FirstName;
            stu.LastName = data.LastName;
            stu.BirthDay = (DateTime)data.Birthday;
            stu.Email = data.Email;
            stu.ClassID = data.ClassID;
            //Update database
            _context.SubmitChanges();
            _context.Dispose();
            return true;
        }

        public bool DeleteData(int Id)
        {
            LibraryEntitiesDataContext _context = base.GetDataContext();
            //Code
            Library_Student stu = _context.Library_Students.Where(s => s.Id == Id).FirstOrDefault();
            //Delete in database
            _context.Library_Students.DeleteOnSubmit(stu);
            _context.SubmitChanges();
            //
            _context.Dispose();
            return true;
        }
    }
}